const {
  query,
  queryOne,
  exec
} = require('../db/mysql')
const {
  md5
} = require('../utils/index')
//添加数据
const add = (user) => {
  user.userpassword = md5(user.userpassword);
  user.status = true;
  user.nickName = user.nickName ? user.nickName : '';
  user.realName = user.realName ? user.realName : '';
  let nowDate = new Date();
  user.addTime = nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + " " + nowDate.getHours() + ":" + nowDate.getMinutes() + ":" + nowDate.getSeconds();
  let sql = `insert into user (userName,userpassword,nickName,realName,addTime,status) values ('${user.userName}','${user.userpassword}','${user.nickName}','${user.realName}','${user.addTime}',${user.status})`

 return exec(sql);
}
//查询单条信息
const getUser = (username) => {
  let sql = `select * from user where username='${username}'`;
 return queryOne(sql);
}
//分页查询里列表
const getList = () => {
  let sql = "select1 userName,nickName,realName,status from user";
  return query(sql);
}
//修改密码
const updatePwd=(username,userpassword)=>{
 userpassword= md5(userpassword);
const sql=`update user set userpassword='${userpassword}' where userName='${username}'` 
return exec(sql);
}
//根据用户名删除用户
const delUser=(username)=>{
  const sql=`delete from user where userName='${username}'`;
  return exec(sql);
}

module.exports = {
  add,
  getUser,
  getList,
  updatePwd,
  delUser


}